<template>
  <ContentWrap v-loading="formLoading">
    <el-form ref="formRef" :model="formData" :rules="formRules" label-width="100px" v-loading="formLoading">
      <!-- <el-form-item label="父级ID" prop="parentId">
        <el-input v-model="formData.parentId" placeholder="请输入父级ID" />
      </el-form-item>
      <el-form-item label="类型状态" prop="status">
        <el-radio-group v-model="formData.status">
          <el-radio label="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="深度" prop="depth">
        <el-input-number v-model="formData.depth" placeholder="请输入深度" />
      </el-form-item> -->
      <el-form-item label="类型名称" prop="typeName">
        <el-input v-model="formData.typeName" placeholder="请输入类型名称" />
      </el-form-item>
      <el-form-item label="是否可见" prop="visible">
        <el-radio-group v-model="formData.visible">
          <el-radio :key="dict.value" :label="dict.value" v-for="dict in getIntDictOptions(DICT_TYPE.IS_NO)">{{dict.label }}</el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item label="显示顺序" prop="sort">
        <el-input-number v-model="formData.sort" placeholder="请输入顺序" />
      </el-form-item>
      <el-divider border-style="dashed">字典选项配置（以下选项在 系统管理->字典管理 中配置）</el-divider>

      <el-form-item label="度数" prop="degrees">
        <el-select multiple v-model="formData.degrees">
          <el-option v-for="dict in getIntDictOptions(DICT_TYPE.DEGREE) " :key="dict.label" :label="dict.label"
            :value="dict.label" />
        </el-select>
      </el-form-item>
      <el-form-item label="年份" prop="showYears">
        <el-select multiple v-model="formData.showYears">
          <el-option v-for="dict in getIntDictOptions(DICT_TYPE.YEAR)  " :key="dict.label" :label="dict.label"
            :value="dict.label" />
        </el-select>

      </el-form-item>
      <el-form-item label="香型" prop="fragranceTypes">
        <el-select multiple v-model="formData.fragranceTypes">
          <el-option v-for="dict in getIntDictOptions(DICT_TYPE.ODOR_TYPE) " :key="dict.label" :label="dict.label"
            :value="dict.label" />
        </el-select>

      </el-form-item>
      <el-form-item label="容量" prop="capacities">
        <el-select multiple v-model="formData.capacities">
          <el-option v-for="dict in getIntDictOptions(DICT_TYPE.CAPACITY) " :key="dict.label" :label="dict.label"
            :value="dict.label" />
        </el-select>

      </el-form-item>
      <el-form-item label="单位" prop="units">
        <el-select multiple v-model="formData.units">
          <el-option v-for="dict in getIntDictOptions(DICT_TYPE.UNIT) " :key="dict.label" :label="dict.label"
            :value="dict.label" />
        </el-select>

      </el-form-item>
      <el-form-item label="标签" prop="labels">
        <el-select multiple v-model="formData.labels">
          <el-option v-for="dict in getIntDictOptions(DICT_TYPE.SUPPLY_AND_DEMAND_LABEL)" :key="dict.label"
            :label="dict.label" :value="dict.label" />
        </el-select>
      </el-form-item>
      <el-form-item label="包装" prop="packings">
        <el-select multiple v-model="formData.packings">
          <el-option v-for="dict in getIntDictOptions(DICT_TYPE.PACKING) " :key="dict.label" :label="dict.label"
            :value="dict.label" />
        </el-select>
      </el-form-item>
      <el-form-item label="语言" prop="languages">
        <el-select multiple v-model="formData.languages">
          <el-option v-for="dict in getIntDictOptions(DICT_TYPE.LANGUAGE) " :key="dict.label" :label="dict.label"
            :value="dict.label" />
        </el-select>
      </el-form-item>
      <el-form-item label="产地" prop="producingAreas">
        <el-select multiple v-model="formData.producingAreas">
          <el-option v-for="dict in getIntDictOptions(DICT_TYPE.PRODUCING_AREA) " :key="dict.label" :label="dict.label"
            :value="dict.label" />
        </el-select>
      </el-form-item>

    </el-form>
    <el-form>
      <el-form-item style="float: right">
        <el-button :loading="formLoading" type="primary" @click="submitForm">
          保存
        </el-button>
        <el-button @click="close">返回</el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>
</template>
<script setup lang="ts">
import { SupplyTypeApi, SupplyTypeVO } from '@/api/wine/supplytype'
import { useTagsViewStore } from '@/store/modules/tagsView'
import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'

/** 酒类类型 表单 */
defineOptions({ name: 'SupplyTypeForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗
const { push, currentRoute } = useRouter() // 路由
const { params } = useRoute() // 查询参数
const { delView } = useTagsViewStore() // 视图操作
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formData = ref({
  id: undefined,
  parentId: undefined,
  typeName: undefined,
  depth: 1,
  visible: 1,
  sort: 1,
  degrees: undefined,
  showYears: undefined,
  fragranceTypes: undefined,
  capacities: undefined,
  units: undefined,
  labels: undefined,
  packings: undefined,
  producingAreas: undefined,
  languages: undefined

})
const formRules = reactive({
  typeName: [{ required: true, message: '请输入类型名称', trigger: 'blur' }],
  depth: [{ required: true, message: '请输入深度', trigger: 'blur' }],
  visible: [{ required: true, message: '请选择是否可见', trigger: 'change' }],
  status: [{ required: true, message: '请选择类型状态', trigger: 'change' }],
  sort: [{ required: true, message: '请选择显示顺序', trigger: 'sort' }],

})
const formRef = ref() // 表单 Ref


const getDetail = async () => {
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      let data = await SupplyTypeApi.getSupplyType(id)
      data.degrees = data.degrees ? data.degrees.split(',') : []
      data.showYears = data.showYears ? data.showYears.split(',') : []
      data.fragranceTypes = data.fragranceTypes ? data.fragranceTypes.split(',') : []
      data.capacities = data.capacities ? data.capacities.split(',') : []
      data.units = data.units ? data.units.split(',') : []
      data.labels = data.labels ? data.labels.split(',') : []
      data.packings = data.packings ? data.packings.split(',') : []
      data.producingAreas = data.producingAreas ? data.producingAreas.split(',') : []
      data.languages = data.languages ? data.languages.split(',') : []
      formData.value = data

    } finally {
      formLoading.value = false
    }
  }
}

onMounted(async () => {
  await getDetail()
})
const id = params.id as unknown as number

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {



    if (formData.value.degrees && Array.isArray(formData.value.degrees)) {
      formData.value.degrees = formData.value.degrees.join(',')
    } else {
      formData.value.degrees = ''

    }

    if (formData.value.showYears && Array.isArray(formData.value.showYears)) {
      formData.value.showYears = formData.value.showYears.join(',')
    } else {
      formData.value.showYears = ''

    }

    if (formData.value.fragranceTypes && Array.isArray(formData.value.fragranceTypes)) {
      formData.value.fragranceTypes = formData.value.fragranceTypes.join(',')
    } else {
      formData.value.fragranceTypes = ''

    }

    if (formData.value.capacities && Array.isArray(formData.value.capacities)) {
      formData.value.capacities = formData.value.capacities.join(',')
    } else {
      formData.value.capacities = ''

    }

    if (formData.value.units && Array.isArray(formData.value.units)) {
      formData.value.units = formData.value.units.join(',')
    } else {
      formData.value.units = ''

    }

    if (formData.value.labels && Array.isArray(formData.value.labels)) {
      formData.value.labels = formData.value.labels.join(',')
    } else {
      formData.value.labels = ''

    }
    if (formData.value.packings && Array.isArray(formData.value.packings)) {
      formData.value.packings = formData.value.packings.join(',')
    } else {
      formData.value.packings = ''

    }
    if (formData.value.producingAreas && Array.isArray(formData.value.producingAreas)) {
      formData.value.producingAreas = formData.value.producingAreas.join(',')
    } else {
      formData.value.producingAreas = ''

    }
    if (formData.value.languages && Array.isArray(formData.value.languages)) {
      formData.value.languages = formData.value.languages.join(',')
    } else {
      formData.value.languages = ''

    }


    const data = formData.value as unknown as SupplyTypeVO
    if (!id) {
      await SupplyTypeApi.createSupplyType(data)
      message.success(t('common.createSuccess'))
    } else {
      await SupplyTypeApi.updateSupplyType(data)
      message.success(t('common.updateSuccess'))
    }
    close()
  } finally {
    formLoading.value = false
  }
}
/** 关闭按钮 */
const close = () => {
  delView(unref(currentRoute))
  push({ name: 'SupplyType' })
}
/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    parentId: undefined,
    typeName: undefined,
    depth: 1,
    visible: 1,
    sort: 1
  }
  formRef.value?.resetFields()
}
</script>
